package com.cyj.dream.rabbitmq.config;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @Description: 消息队列消息对象--消息发布者:MessagePublisher
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.rabbitmq.config
 * @Author: ChenYongJia
 * @CreateTime: 2021-10-19 13:30
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Getter
@Setter
@ToString
public class RabbitMqMessage {
    /**
     * 主机名的环境变量名
     */
    private static final String HOSTNAME_ENV_NAME = "HOSTNAME";

    /**
     * 主机名
     */
    private String hostName;

    /**
     * 转发器名称
     */
    private String exchangeName;

    /**
     * 消息
     */
    private String message;

    private RabbitMqMessage() {
        // 禁止实例化，避免生成没有主机名的消息
    }

    public static RabbitMqMessage newMessage(String exchangeName, String message) {
        RabbitMqMessage msg = new RabbitMqMessage();
        msg.setExchangeName(exchangeName);
        msg.setMessage(message);
        // 在消息上附加主机名，便于问题追踪
        String hostName = "unknown";
        try {
            hostName = System.getenv(HOSTNAME_ENV_NAME);
        } catch (Exception e) {
            // nothing to do
        }
        msg.setHostName(hostName);
        return msg;
    }

}
